package com.xinke.module.system.service.notice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.notice.*;
import com.xinke.module.system.model.entity.notice.NoticeDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.notice.NoticeMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 通知公告 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public Long createNotice(NoticeSaveReqVO createReqVO) {
        // 插入
        NoticeDO notice = BeanUtils.toBean(createReqVO, NoticeDO.class);
        noticeMapper.insert(notice);
        // 返回
        return notice.getNoticeId();
    }

    @Override
    public void updateNotice(NoticeSaveReqVO updateReqVO) {
        // 校验存在
        validateNoticeExists(updateReqVO.getNoticeId());
        // 更新
        NoticeDO updateObj = BeanUtils.toBean(updateReqVO, NoticeDO.class);
        noticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteNotice(Long noticeId) {
        // 校验存在
        validateNoticeExists(noticeId);
        // 删除
        noticeMapper.deleteById(noticeId);
    }

    private void validateNoticeExists(Long noticeId) {
        if (noticeMapper.selectById(noticeId) == null) {
            throw exception(null);
        }
    }

    @Override
    public NoticeDO getNotice(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    public PageResult<NoticeDO> getNoticePage(NoticePageReqVO pageReqVO) {
        return noticeMapper.selectPage(pageReqVO);
    }

}